CMAKE_MINIMUM_REQUIRED(VERSION 2.8.4)
PROJECT(sheetmerge)

# parameters
OPTION(BUILD_STATIC "Build static libraries" ON)
OPTION(BUILD_SHARED "Build shared libraries" ON)
OPTION(WITH_LIBZIP "Use libzip instead of Minizip" ON)
OPTION(WITH_WIDE "Also build UTF-16 library (libxlsxio_readw)" OFF)
SET(ZLIB_DIR "" CACHE PATH "Path to the zlib library")
SET(SQLITE3_SRCDIR      ${CMAKE_CURRENT_SOURCE_DIR}/sqlite-autoconf-3250200 )

IF(WITH_LIBZIP)
  SET(LIBZIP_DIR "" CACHE PATH "Path to the libzip library")
ELSE()
  SET(MINIZIP_DIR "" CACHE PATH "Path to the Minizip library")
ENDIF()
SET(EXPAT_DIR "" CACHE PATH "Path to the Expat library")

# conditions
IF(NOT BUILD_STATIC AND NOT BUILD_SHARED)
  MESSAGE(FATAL_ERROR "Cannot build with both BUILD_STATIC and BUILD_SHARED disabled")
ENDIF()

# dependancies
SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake" ${CMAKE_MODULE_PATH})
#   dependancy: zlib (dependancy of libzip/Minizip, should be detected there)
IF(ZLIB_DIR)
  SET(ZLIB_ROOT ${ZLIB_DIR})
ENDIF()
#FIND_PACKAGE(ZLIB REQUIRED)
#   dependancy: libzip
IF(WITH_LIBZIP)
  FIND_PACKAGE(LibZip REQUIRED)
  SET(ANYZIP_INCLUDE_DIRS ${LIBZIP_INCLUDE_DIRS})
  SET(ANYZIP_LIBRARIES ${LIBZIP_LIBRARIES})
  SET(ANYZIP_DEF USE_LIBZIP)
ELSE()
  FIND_PACKAGE(Minizip REQUIRED)
  SET(ANYZIP_INCLUDE_DIRS ${MINIZIP_INCLUDE_DIRS})
  SET(ANYZIP_LIBRARIES ${MINIZIP_LIBRARIES})
  SET(ANYZIP_DEF USE_MINIZIP)
ENDIF()
#   dependancy: expat
IF(EXPAT_DIR)
  #FIND_PACKAGE(EXPAT MODULE REQUIRED NO_DEFAULT_PATH PATHS ${EXPAT_DIR})
  FIND_PATH(EXPAT_INCLUDE_DIR NAMES expat.h NO_DEFAULT_PATH PATHS ${EXPAT_DIR}/include ${EXPAT_DIR})
  FIND_LIBRARY(EXPAT_LIBRARIES NAMES expat libexpat NO_DEFAULT_PATH PATHS ${EXPAT_DIR}/lib ${EXPAT_DIR})
ELSE()
  FIND_PACKAGE(EXPAT REQUIRED)
ENDIF()
#   dependancy: expatw (if wide library was requested)
IF(WITH_WIDE)
  IF(EXPAT_DIR)
    FIND_LIBRARY(EXPATW_LIBRARIES NAMES expatw libexpatw NO_DEFAULT_PATH PATHS ${EXPAT_DIR}/lib ${EXPAT_DIR})
  ELSE()
    FIND_LIBRARY(EXPATW_LIBRARIES NAMES expatw)
  ENDIF()
ENDIF()

# build parameters
#SET(CMAKE_CXX_FLAGS "-g -Wall")
#ADD_SUBDIRECTORY(src)

IF(NOT WIN32 AND NOT APPLE)
  SET(THREADLIB "-pthread")
ENDIF()

INCLUDE_DIRECTORIES(xlsxio/include ${SQLITE3_SRCDIR})
INCLUDE_DIRECTORIES(${ANYZIP_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${EXPAT_INCLUDE_DIRS})

# build definitions
SET(ALLTARGETS)
SET(LINKTYPES)
IF(BUILD_STATIC)
  LIST(APPEND LINKTYPES "STATIC")
ENDIF()
IF(BUILD_SHARED)
  LIST(APPEND LINKTYPES "SHARED")
ENDIF()

FOREACH(LINKTYPE ${LINKTYPES})
  ADD_LIBRARY(xlsxio_read_${LINKTYPE} ${LINKTYPE} xlsxio/lib/xlsxio_read.c xlsxio/lib/xlsxio_read_sharedstrings.c)
  SET_TARGET_PROPERTIES(xlsxio_read_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
  SET_TARGET_PROPERTIES(xlsxio_read_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "BUILD_XLSXIO;${ANYZIP_DEF}")
  SET_TARGET_PROPERTIES(xlsxio_read_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_read)
  TARGET_INCLUDE_DIRECTORIES(xlsxio_read_${LINKTYPE} PRIVATE xlsxio/lib)
  TARGET_LINK_LIBRARIES(xlsxio_read_${LINKTYPE} ${ANYZIP_LIBRARIES} ${EXPAT_LIBRARIES})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_read_${LINKTYPE})

  ADD_LIBRARY(xlsxio_write_${LINKTYPE} ${LINKTYPE} xlsxio/lib/xlsxio_write.c)
  SET_TARGET_PROPERTIES(xlsxio_write_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
  SET_TARGET_PROPERTIES(xlsxio_write_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "BUILD_XLSXIO;${ANYZIP_DEF}")
  SET_TARGET_PROPERTIES(xlsxio_write_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_write)
  TARGET_LINK_LIBRARIES(xlsxio_write_${LINKTYPE} ${ANYZIP_LIBRARIES} ${THREADLIB})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_write_${LINKTYPE})

  IF(WITH_WIDE)
    ADD_LIBRARY(xlsxio_readw_${LINKTYPE} ${LINKTYPE} xlsxio/lib/xlsxio_read.c xlsxio/lib/xlsxio_read_sharedstrings.c)
    SET_TARGET_PROPERTIES(xlsxio_readw_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
    SET_TARGET_PROPERTIES(xlsxio_readw_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "XML_UNICODE;BUILD_XLSXIO;${ANYZIP_DEF}")
    SET_TARGET_PROPERTIES(xlsxio_readw_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_readw)
    TARGET_INCLUDE_DIRECTORIES(xlsxio_readw_${LINKTYPE} PRIVATE xlsxio/lib)
    TARGET_LINK_LIBRARIES(xlsxio_readw_${LINKTYPE} ${ANYZIP_LIBRARIES} ${EXPATW_LIBRARIES})
    SET(ALLTARGETS ${ALLTARGETS} xlsxio_readw_${LINKTYPE})

    #ADD_LIBRARY(xlsxio_writew_${LINKTYPE} ${LINKTYPE} xlsxio/lib/xlsxio_write.c)
    #SET_TARGET_PROPERTIES(xlsxio_writew_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
    #SET_TARGET_PROPERTIES(xlsxio_writew_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "XML_UNICODE;BUILD_XLSXIO;${ANYZIP_DEF}")
    #SET_TARGET_PROPERTIES(xlsxio_writew_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_write_w)
    #TARGET_LINK_LIBRARIES(xlsxio_writew_${LINKTYPE} ${ANYZIP_LIBRARIES} ${THREADLIB})
    #SET(ALLTARGETS ${ALLTARGETS} xlsxio_writew_${LINKTYPE})
  ENDIF()

  SET(EXELINKTYPE ${LINKTYPE})
ENDFOREACH()



  ADD_LIBRARY(sqlite3_static STATIC ${SQLITE3_SRCDIR}/sqlite3.c)
  SET_TARGET_PROPERTIES(sqlite3_static PROPERTIES OUTPUT_NAME "sqlite3")

  ADD_EXECUTABLE(XLSX XLSX.c BIT_ARRAY.c cJSON.c cfgfile.c)
  TARGET_LINK_LIBRARIES(XLSX -lsqlite3 xlsxio_read_${EXELINKTYPE} xlsxio_write_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} XLSX)
